package com.cwises.action.withdraw;

import java.util.Date;
import java.util.UUID;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;

import com.core.base.BaseAction;
import com.cwises.entity.shop.ShopData;
import com.cwises.entity.statistics.Statistics;
import com.cwises.entity.withdraw.Withdraw;
import com.cwises.service.withdraw.WithdrawService;
import com.orm.Page;

@Controller
public class WithdrawAction extends BaseAction {

	@Resource(name = "withdrawService")
	private WithdrawService withdrawService;

	private Withdraw withdraw;
	private Statistics statistics;
	private Page<Withdraw> page;

	/* 当前页号及每页数 */
	private int currentPage;
	private int pageSize;

	private String actionResult;
	private String actionTitle = "提现管理";

	public String findList() {
		page = new Page<Withdraw>();
		// 设置当前页
		page.setCurrentPage(currentPage);
		String shopId=getShopId();
		if(shopId!=null&&!"".equals(shopId)){
			if(withdraw==null){
				withdraw=new Withdraw();
			}
			withdraw.setShop_id(shopId);
			page = withdrawService.findByCondition("findAll", withdraw, page);
			//查询已兑换积分总数及剩余未兑换积分数
			statistics=withdrawService.findObjectByCondition("WithdrawStatistics", withdraw);
			if(statistics==null){
				statistics=new Statistics();
			}else{
				statistics.setWt_score(statistics.getOrder_score()-statistics.getYt_score());
				statistics.setWt_gold(statistics.getOrder_gold()-statistics.getYt_gold());
			}
		}
		return "success";
	}
	
	/**
	 * @Description: 详情页面
	 * @return String
	 * @throws
	 */
	public String gotoDetail() {
		// 获取前台传递的id get方式传值
		String id = getParameter("id");
		// 查询实体 对应map中findById
		withdraw = (Withdraw) withdrawService.findById(id);
		if("1".equals(withdraw.getStatus())){
			String shopId=getShopId();
			ShopData shopData=withdrawService.findObjectByCondition("findShopData",shopId);
			withdraw.setShopData(shopData);
		}else{
			String account=withdraw.getBank_account();
			withdraw.setBank_account(account.substring(0,3)+"******"+account.substring(account.length()-3));
		}
		return "success";
	}
	/**
	 * @Description: 提积分申请页面
	 * @return String
	 * @throws
	 */
	public String insert() {
		String flag = "success";
		try {
			withdraw.setId(UUID.randomUUID().toString());
			withdraw.setShop_id(getShopId());
			withdraw.setCreate_person(getCurUserId());
			withdraw.setCreate_time(new Date());
			withdraw.setStatus("1");
			// 对应map中update
			withdrawService.addEntity(withdraw);
		} catch (Exception e) {
			flag = "error";
			e.printStackTrace();
		}
		setActionResult(flag);
		return "success";
	}
	/**
	 * @Description: 提代金币申请页面
	 * @param @return
	 * @param @throws Exception
	 * @return String
	 * @throws
	 */
	public String gotoAddGold() {
		String shopId=getShopId();
		ShopData shopData=withdrawService.findObjectByCondition("findShopData",shopId);
		//查询已兑换积分总数及剩余未兑换积分数
		Statistics statistics=withdrawService.findObjectByCondition("StatisticsGold", shopId);
		withdraw=new Withdraw(shopData);
		if(statistics==null){
			withdraw.setW_gold(0);
			withdraw.setAmount(0);
		}else{
			withdraw.setW_gold((int)(statistics.getOrder_gold()-statistics.getYt_gold()));
			double sx=0;
			if(withdraw.getW_gold()>=10){
				sx=withdraw.getW_gold()/100;
			}
			withdraw.setAmount(withdraw.getW_gold()/10-sx);
		}
		return "success";
	}
	
	/**
	 * @Description: 提积分申请页面
	 * @return String
	 * @throws
	 */
	public String gotoAddScore() {
		String shopId=getShopId();
		ShopData shopData=withdrawService.findObjectByCondition("findShopData",shopId);
		//查询已兑换积分总数及剩余未兑换积分数
		Statistics statistics=withdrawService.findObjectByCondition("StatisticsScore", shopId);
		withdraw=new Withdraw(shopData);
		if(statistics==null){
			withdraw.setW_score(0);
			withdraw.setAmount(0);
		}else{
			withdraw.setW_score(statistics.getOrder_score()-statistics.getYt_score());
			withdraw.setAmount(withdraw.getW_score());
		}
		return "success";
	}
	
	public Withdraw getWithdraw() {
		return withdraw;
	}

	public void setWithdraw(Withdraw withdraw) {
		this.withdraw = withdraw;
	}

	public Page<Withdraw> getPage() {
		return page;
	}

	public void setPage(Page<Withdraw> page) {
		this.page = page;
	}

	public int getCurrentPage() {
		return currentPage;
	}

	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public String getActionResult() {
		return actionResult;
	}

	public void setActionResult(String actionResult) {
		this.actionResult = actionResult;
	}

	public String getActionTitle() {
		return actionTitle;
	}

	public void setActionTitle(String actionTitle) {
		this.actionTitle = actionTitle;
	}

	public Statistics getStatistics() {
		return statistics;
	}

	public void setStatistics(Statistics statistics) {
		this.statistics = statistics;
	}

}
